package com.tomato.tv.activity

import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.SurfaceHolder
import com.tomato.tv.R
import kotlinx.android.synthetic.main.activity_media_player.*
import tv.danmaku.ijk.media.player.IjkMediaPlayer

class IJKPlayerActivity : AppCompatActivity() {

    lateinit var ijkMediaPlayer: IjkMediaPlayer

    //private val url = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4"
   // private val url = "https://media.w3.org/2010/05/sintel/trailer.mp4"
   // private val url = "rtsp://192.168.4.148:8554/lonbon"
    private val url = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ijkplayer)

        initPlayer()
    }

    private fun initPlayer() {
        ijkMediaPlayer = IjkMediaPlayer()
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "dns_cache_clear", 1);

        surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceCreated(holder: SurfaceHolder) {
                ijkMediaPlayer.setDisplay(holder)

                ijkMediaPlayer.setDataSource(applicationContext,Uri.parse(url))
                ijkMediaPlayer._prepareAsync()
                ijkMediaPlayer.setOnPreparedListener {
                    it.start()
                }
            }

            override fun surfaceChanged(
                holder: SurfaceHolder,
                format: Int,
                width: Int,
                height: Int
            ) {
            }
            override fun surfaceDestroyed(holder: SurfaceHolder) {
            }
        })
    }
}